package training_set;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;


public class Related_Pairs {
	HashMap<String,Integer> idindex = new HashMap<String,Integer>();
	List<String> idlist = new ArrayList<String>();
	public void ReadIDlist(String filename)
	{
		File f = new File(filename);
		FileInputStream fis;
		try {
			fis = new FileInputStream(f);
			InputStreamReader isr=new InputStreamReader(fis);
			BufferedReader br=new BufferedReader(isr);
			String line = br.readLine();
			int count = 0;
			while(line != null)
			{
				line = line.trim();
				if(line.length()>0)
				{
					idindex.put(line, count++);// build an index for the ids
					idlist.add(line);// keep a record of ids.
				}
				line = br.readLine();
			}
			br.close();
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		
	}
	public List<String> RelatedPair(String idfile)
	{
		ReadIDlist(idfile);// read the idfile firstly.
		List<String> pairs = new ArrayList<String>();
		try{
			
			for(int i=0;i<idlist.size();i++)
			{
				// try to find current id's related pair.
				String related = FindRelated(idlist.get(i));
				if(related != null)
				{
					int j = 0;
					for(j=0;j<pairs.size();j++)
					{
						String s[] = pairs.get(j).split("\t");
						if(s[0].equals(idlist.get(i))&& s[1].equals(related))
							break;
						if(s[0].equals(related)&&s[1].equals(idlist.get(i)))
							break;
					}
					if(j==pairs.size())
					{
						pairs.add(idlist.get(i)+"\t"+related);
					}
				}
				Thread t = new Thread();
				Thread.sleep(500);
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pairs;
	}
	public String FindRelated(String id)
	{
		String related = null;
		try {
			URL url = new URL("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi?dbfrom=pubmed&db=pubmed&id="+id);
			HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
		    InputStream inputStream = httpConnection.getInputStream();
		    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
		    String line = br.readLine();
		    int count=0;
		    while(line != null)
		    {
		    	if(line.contains("<Id>"))
		    	{
		    		int index = line.indexOf(">");
		    		int end = line.lastIndexOf("<");
		    		String tempid = line.substring(index+1, end);
		    		if(!tempid.equals(id))
		    		{
		    			count++;
		    			if(idindex.containsKey(tempid))// find the most related one.
		    			{
		    				related = tempid;
		    				return related;
		    			}
		    			if(count>=20)// only check the top 20.
		    				return related;

		    		}
		    	}
		    	line = br.readLine();
		    }
		    br.close();
		    httpConnection.disconnect();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return related;
	}

}
